#pragma once

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <string>
#include <iostream>
#include <sstream>
#include "Util.hpp"

enum
{
    DIV_ERRO = 1,
    MOD_ERRO,
    OPERA_ERRO
};

const std::string stap = "\r\n";
const std::string dufault_path = "./wwwroot";
const std::string home_page = "index.html";
const std::string html_404 = "./wwwroot/404.html";

class httpRequest
{
public:
    /// @brief 解析inbuffer内容
    /// method url version
    void paser()
    {
        std::string line = Util::getline(inbuffer, stap);
        if (line.empty())
            return;
#ifdef DEBUG
        std::cout << "line" << line << std::endl;
#endif
        std::stringstream ss(line);
        ss >> method >> url >> version; // 默认不接受空字符，以空字符分割

        // 获取资源地址
        path = dufault_path; // ./wwwroot
        path += url;         // ./wwwroot/  or ./wwwroot/a/b/c
        if (path[path.size() - 1] == '/')
            path += home_page;

        // 获取资源对应的后缀
        auto pos = path.rfind('.');
        suffix = path.substr(pos);

        // 获取资源大小
        struct stat st;
        int n = stat(path.c_str(), &st);
        if (n == 0)
            lenth = st.st_size;
        else
            lenth = -1;
    }

public:
    std::string inbuffer;

    std::string method;  // 执行方法
    std::string url;     // url
    std::string version; // 版本信息
    std::string path;    // 执行方法的路径
    std::string suffix;  // 请求资源后缀
    int lenth;           // 资源大小

};

class httpResponse
{
public:
    std::string outbuffer;
};